import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


public class StudentHomePanel extends JPanel{
        JPanel panel;
        JPanel north, center;
        JTable programT,scheduleT;
        String[] programCols={"Programs","CumulativeGPA","Progress"};
        Object[][] programDatas={};
        String[] scheduleCols={"","Mon","Tue","Wed","Thu","Fri"};
        Object[][] scheduleDatas={{"8am","","","","",""},
                        {"9am","","","","",""},
                        {"10am","","","","",""},
                        {"11am","","","","",""},
                        {"12am","","","","",""},
                        {"01pm","","","","",""},
                        {"02pm","","","","",""},
                        {"03pm","","","","",""},
                        {"04pm","","","","",""},
                        {"05pm","","","","",""},
                        {"06pm","","","","",""}
        };
        Image img;
        static int totalc;
        static float totalg;
       
        public StudentHomePanel(){
               
//              img=new ImageIcon("studentHome_background.png").getImage();
//              setOpaque(false);
               
                //entire panel
                panel=new JPanel();
                panel.setPreferredSize(new Dimension(600,400));
                panel.setLayout(new BorderLayout());
                panel.setBackground(Color.WHITE);
                panel.setOpaque(false);
               
               
                //initialize the table from database
                initialize();
               
               
                //north panel
                north=new JPanel();
                north.setLayout(new BorderLayout());
                north.setBackground(Color.white);
                //programT=new JTable(programDatas,programCols);
                TableColumn column;
                column = programT.getColumnModel().getColumn(0);
                column.setPreferredWidth(270);
               
               
                north.add(programT.getTableHeader(), BorderLayout.PAGE_START);
                north.add(programT, BorderLayout.CENTER);
               
                JPanel empty=new JPanel();
                empty.setPreferredSize(new Dimension(600,20));
                empty.setBackground(new Color(169,25,25));
                //empty.setOpaque(false);
               
                north.add(empty, BorderLayout.SOUTH);
                north.setOpaque(false);
               
                panel.add(north,BorderLayout.NORTH);
               
               
               
                //center panel
                center=new JPanel();
                center.setLayout(new BorderLayout());
                center.setBackground(Color.white);
               
                JPanel npanel=new JPanel();
                npanel.setBackground(Color.white);
                npanel.setLayout(new BorderLayout());
                JLabel label=new JLabel("Fall Semester 2014 Schedule");
                npanel.add(label,BorderLayout.NORTH);
                center.add(npanel, BorderLayout.NORTH);
               
               
               
                JPanel cpanel=new JPanel();
                cpanel.setBackground(Color.white);
                cpanel.setLayout(new BorderLayout());
               
               
               
                cpanel.add(scheduleT.getTableHeader(), BorderLayout.PAGE_START);
                cpanel.add(scheduleT, BorderLayout.CENTER);
                //cpanel.setOpaque(false);
                center.add(cpanel,BorderLayout.CENTER);
               
                JPanel empty2=new JPanel();
                empty2.setPreferredSize(new Dimension(600,80));
                empty2.setBackground(new Color(169,25,25));
               
                center.add(empty2,BorderLayout.SOUTH);
                //center.setOpaque(false);
               
                panel.add(center,BorderLayout.CENTER);
               
                add(panel);
        }
        
       
        private void initialize(){
                //table model
                DefaultTableModel dtm=new DefaultTableModel(scheduleDatas,scheduleCols);
                scheduleT=new JTable(dtm);
                String course="CS-3370-TDAA";
                int row, col;
                               
               
               
                try{
                        String dName = "com.mysql.jdbc.Driver";
                        String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                        Connection con = DriverManager.getConnection(url);
                        java.sql.Statement mysqlStatement = con.createStatement();
                        //java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT course_name, start_time, days From course_catalog, current_course_total_list WHERE course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
                        java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT ca.course_name, co.start_time, co.days From course_catalog AS ca JOIN current_course_total_list AS co ON ca.CID = co.CID WHERE ca.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
                       
                        //while (resultSet.next()&&resultSet2.next()) {
                                while(resultSet.next()){
                                //String course_number= resultSet.getString(1);
                                String course_name=resultSet.getString(1);
                                //String course_section=resultSet.getString(3);
                                //course=course_number+"-"+course_name+"-"+course_section;
                                course=course_name;
                               
                                int startTime=resultSet.getInt(2);
                                String days=resultSet.getString(3);
                               
                                if(startTime==8){
                                        row=0;
                                }
                                else if(startTime==9){
                                        row=1;
                                }else if(startTime==10){
                                        row=2;
                                }else if(startTime==11){
                                        row=3;
                                }else if(startTime==12){
                                        row=4;
                                }else if(startTime==1){
                                        row=5;
                                }else if(startTime==2){
                                        row=6;
                                }else if(startTime==3){
                                        row=7;
                                }else if(startTime==4){
                                        row=8;
                                }else if(startTime==5){
                                        row=9;
                                }else{
                                        row=10;
                                }
                               
                                if(days.equals("M")){
                                        col=1;
                                }
                                else if(days.equals("T")){
                                        col=2;
                                }else if(days.equals("W")){
                                        col=3;
                                }else if(days.equals("TH")){
                                        col=4;
                                }else{
                                        col=5;
                                }
                               
                                scheduleT.setValueAt(course, row, col);
                               
                                System.out.println(course+ " "+startTime+" "+days+" ");
                               
                               
                        }
                        con.close();
                        }
                        catch(Exception ex){}
               
               
               
                //student data table
                DefaultTableModel dtm2=new DefaultTableModel(programDatas,programCols);
                programT=new JTable(dtm2);
               
                totalc=1;
                totalg=1;
               
               
                try{
                        String dName = "com.mysql.jdbc.Driver";
                        String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                        Connection con = DriverManager.getConnection(url);
                       
                        java.sql.Statement mysqlStatement2 = con.createStatement();
                        java.sql.ResultSet resultSet2 = mysqlStatement2.executeQuery("SELECT grade FROM total_course_list WHERE SID='"+MenuPanel.SID+"'");
                        java.sql.Statement mysqlStatement3 = con.createStatement();
                        java.sql.ResultSet resultSet3 = mysqlStatement3.executeQuery("SELECT credit_hours FROM course_catalog WHERE CID IN (SELECT CID FROM total_course_list WHERE SID='"+MenuPanel.SID+"')");
                        //java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT major, grade, credit_hours FROM course_catalog, student_list, total_course_list WHERE student_list.SID='"+MenuPanel.SID+"'");// CID IN (SELECT CID FROM total_course_list WHERE SID='"+MenuPanel.SID+"')");
                        while (resultSet2.next()&&resultSet3.next()) {
                        String grade= resultSet2.getString(1);
                        int credits=resultSet3.getInt(1);
                       
                       
                        totalc+=credits;
                        if(grade.equals("A")){
                                totalg+=4*credits;
                        }
                        else if(grade.equals("B")){
                                totalg+=3*credits;
                        }
                        else if(grade.equals("C")){
                                totalg+=2*credits;
                        }
                        else if(grade.equals("D")){
                                totalg+=1*credits;
                        }
                       
                        }
                        con.close();
                        }
                        catch(Exception ex){}
               
               
                try{
                        String dName = "com.mysql.jdbc.Driver";
                        String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                        Connection con = DriverManager.getConnection(url);
                        java.sql.Statement mysqlStatement = con.createStatement();
                        java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT major FROM student_list WHERE SID='"+MenuPanel.SID+"'");
                       
                        while (resultSet.next()) {
                        String major=resultSet.getString(1);
                        Object[] program={major,totalg/totalc,totalc+"/120"};
                        dtm2.addRow(program);
                        }
                        con.close();
                        }
                        catch(Exception ex){}
               
        }

       
}
